ifeq (,$(wildcard ../config.mak))
$(error "../config.mak is not present, run configure !")
endif
include ../config.mak

LIBNAME = libplayer
STATIC_LIBNAME = $(LIBNAME).a
SHARED_LIBNAME = $(LIBNAME).so
SHARED_LIBNAME_VERSION = $(SHARED_LIBNAME).$(VERSION)
SHARED_LIBNAME_MAJOR = $(SHARED_LIBNAME).$(shell echo $(VERSION) | cut -f1 -d.)
SHARED_LIBNAME_FLAGS = -shared -Wl,-soname,$(SHARED_LIBNAME_MAJOR)

ifeq ($(BUILD_STATIC),yes)
  BUILD_RULES += lib_static
endif
ifeq ($(BUILD_SHARED),yes)
  BUILD_RULES += lib_shared
  ifeq ($(BUILD_DYLIB),yes)
    SHARED_LIBNAME         = $(LIBNAME).dylib
    SHARED_LIBNAME_VERSION = $(LIBNAME).$(VERSION).dylib
    SHARED_LIBNAME_MAJOR   = $(LIBNAME).$(shell echo $(VERSION) | cut -f1 -d.).dylib
    SHARED_LIBNAME_FLAGS   = -dynamiclib -Wl,-headerpad_max_install_names,-undefined,dynamic_lookup,-install_name,$(SHARED_LIBNAME_VERSION)
  else
    ifeq ($(BUILD_MINGW32),yes)
      SHARED_LIBNAME         = $(LIBNAME)-$(shell echo $(VERSION) | cut -f1 -d.).dll
      SHARED_LIBNAME_VERSION = $(SHARED_LIBNAME)
      SHARED_LIBNAME_MAJOR   = $(SHARED_LIBNAME)
      SHARED_LIBNAME_FLAGS   = -shared -Wl,--out-implib=$(LIBNAME).dll.a -Wl,--export-all-symbols -Wl,--enable-auto-import
    endif
  endif
endif

SRCS = \
	player.c \
	player_internal.c \
	mrl.c \
	mrl_internal.c \
	playlist.c \
	logs.c \
	fifo_queue.c \
	fs_utils.c \
	parse_utils.c \
	event.c \
	event_handler.c \
	supervisor.c \
	wrapper_dummy.c

EXTRADIST = \
	event.h \
	event_handler.h \
	fifo_queue.h \
	fs_utils.h \
	logs.h \
	parse_utils.h \
	player.h \
	player_internals.h \
	playlist.h \
	supervisor.h \
	wrapper_dummy.h \
	wrapper_gstreamer.h \
	wrapper_mplayer.h \
	wrapper_vlc.h \
	wrapper_xine.h \
	x11_common.h \

SRCS-$(X11)			+= x11_common.c
SRCS-$(WRAPPER_MPLAYER)		+= wrapper_mplayer.c
SRCS-$(WRAPPER_VLC)		+= wrapper_vlc.c
SRCS-$(WRAPPER_XINE)		+= wrapper_xine.c
SRCS-$(WRAPPER_GSTREAMER)	+= wrapper_gstreamer.c

SRCS += $(SRCS-yes)

OBJS = $(SRCS:.c=.o)

.SUFFIXES: .c .o

all: depend $(BUILD_RULES)

.c.o:
	$(CC) -c $(OPTFLAGS) $(CFLAGS) $(CFG_CPPFLAGS) $(CPPFLAGS) -o $@ $<

lib_static: $(STATIC_LIBNAME)

lib_shared: $(SHARED_LIBNAME)

$(STATIC_LIBNAME): $(OBJS)
	$(AR) r $(STATIC_LIBNAME) $(OBJS)
	$(RANLIB) $(STATIC_LIBNAME)

$(SHARED_LIBNAME): $(OBJS)
	$(CC) $(SHARED_LIBNAME_FLAGS) \
	  $(OBJS) $(CFG_LDFLAGS) $(EXTRALIBS) $(LDFLAGS) -o $(SHARED_LIBNAME_VERSION)
	if [ $(BUILD_MINGW32) = no ]; then \
	  $(LN) -sf $(SHARED_LIBNAME_VERSION) $(SHARED_LIBNAME_MAJOR); \
	  $(LN) -sf $(SHARED_LIBNAME_MAJOR) $(SHARED_LIBNAME); \
	fi

clean:
	rm -f *.o *.a *.so* *.dll
	rm -f .depend

install: $(BUILD_RULES)
	$(INSTALL) -d $(libdir)
	[ $(BUILD_STATIC) = yes ] && $(INSTALL) -c $(STATIC_LIBNAME) $(libdir); \
	if [ $(BUILD_SHARED) = yes ]; then \
	  if [ $(BUILD_MINGW32) = no ]; then \
	    $(INSTALL) -c $(SHARED_LIBNAME_VERSION) $(libdir); \
	    $(LN) -sf $(SHARED_LIBNAME_VERSION) $(libdir)/$(SHARED_LIBNAME_MAJOR); \
	    $(LN) -sf $(SHARED_LIBNAME_MAJOR) $(libdir)/$(SHARED_LIBNAME); \
	  else \
	    $(INSTALL) -c $(SHARED_LIBNAME_VERSION) $(bindir); \
	    $(INSTALL) -c $(LIBNAME).dll.a $(libdir); \
	  fi \
	fi
	$(INSTALL) -d $(includedir)
	$(INSTALL) -c -m 644 player.h $(includedir)

uninstall:
	if [ $(BUILD_MINGW32) = yes ]; then \
	  rm -f $(bindir)/$(SHARED_LIBNAME); \
	  rm -f $(libdir)/$(LIBNAME).dll.a; \
	fi
	rm -f $(libdir)/$(STATIC_LIBNAME)
	rm -f $(libdir)/$(SHARED_LIBNAME)*
	rm -f $(includedir)/player.h

depend:
	$(CC) -MM $(CFLAGS) $(CFG_CPPFLAGS) $(CPPFLAGS) $(SRCS) 1>.depend

.PHONY: clean depend
.PHONY: uninstall

dist-all:
	cp $(EXTRADIST) $(SRCS) $(SRCS-no) Makefile $(DIST)

.PHONY: dist-all

#
# include dependency files if they exist
#
ifneq ($(wildcard .depend),)
include .depend
endif
